package cn.dt.ymcc.listener;


import cn.dt.ymcc.domain.PayOrder;
import cn.dt.ymcc.dto.PayNotifyDto;
import cn.dt.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@Component
@RocketMQTransactionListener(txProducerGroup ="sever-pay")
public class PayRocketMQListener implements RocketMQLocalTransactionListener {
    @Autowired
    private IPayOrderService payOrderService;
    /**
     * 执行本地事务（操作数据库都在这里执行）
     * 将
     * @param message t_prder_pay的数据
     * @param org 存的是主订单和子订单需要保存到数据库中的数据
     * @return
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object org) {
        if(org==null){
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        try {
            PayOrder pay = (PayOrder) org;
            Integer payStatus = pay.getPayStatus();
            String orderNo = pay.getOrderNo();
            PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
            //状态不为待支付就回滚，查不到订单也回滚，但是一般不可能，除非有人搞事
            if(payOrder==null||payStatus!=PayOrder.STATUS_PAY_WAIT){
                return RocketMQLocalTransactionState.ROLLBACK;
            }
            //修改支付单状态
            payOrder.setPayStatus(PayOrder.STATUS_PAY_SUCCESS);
            payOrder.setUpdateTime(new Date());
            payOrderService.updateById(payOrder);

        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        return RocketMQLocalTransactionState.COMMIT;
    }

    /**
     * 检查本地事务
     * 检查支付状态是否为已完成，
     * courseOrderService
     * courseOrderItemService
     * @param message(进行了jsonString化，要转成对象还要转换成dto)
     * @return
     */
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        byte[] bytes = (byte[]) message.getPayload();
        String dtoStr = new String(bytes, StandardCharsets.UTF_8);
        PayNotifyDto payNotifyDto = JSONObject.parseObject(dtoStr, PayNotifyDto.class);
        if(payNotifyDto==null){
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        try {
            String orderNo = payNotifyDto.getOrderNo();
            PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
            if(payOrder==null){
                return RocketMQLocalTransactionState.UNKNOWN;
            }
            Integer payStatus = payOrder.getPayStatus();
            if(payStatus!=PayOrder.STATUS_PAY_SUCCESS){
                return RocketMQLocalTransactionState.ROLLBACK;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        return RocketMQLocalTransactionState.COMMIT;
    }
}
